package spring.boot.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import spring.boot.admin.model.TCommonConfig;
import spring.boot.admin.service.TCommonConfigService;


@Controller
@RequestMapping("/t/common/config")
public class TCommonConfigController {

    @Autowired
    private TCommonConfigService tCommonConfigService;


    /**
     * 跳转到管理页面
     */
    @GetMapping("to/list")
    @PreAuthorize("hasRole('T_COMMON_CONFIG_LIST')")
    public String toList() {
        return "common/config/list";
    }


    /**
     * 分页查询
     */
    @PostMapping("list")
    @ResponseBody
    @PreAuthorize("hasRole('T_COMMON_CONFIG_LIST')")
    public IPage<TCommonConfig> list(Integer page, Integer rows) {
        if (page == null) {
            page = 1;
        }
        if (rows == null) {
            rows = 10;
        }
        return tCommonConfigService.pageList(page, rows);
    }


    /**
     * 新增
     */
    @PostMapping("insert")
    @ResponseBody
    @PreAuthorize("hasRole('T_COMMON_CONFIG_ADD')")
    public String insert(@RequestParam(required = false) String type,
                         @RequestParam(required = false) String typeRemark,
                         @RequestParam(required = false) String keys,
                         @RequestParam(required = false) String keysRemark,
                         @RequestParam(required = false) String value,
                         @RequestParam(required = false) String valueRemark) {
        if (type == null || typeRemark == null || keys == null ||
                keysRemark == null || value == null || valueRemark == null) {
            return "-1";
        }
        return tCommonConfigService.insert(type, typeRemark, keys, keysRemark, value, valueRemark);
    }


    /**
     * 根据主键删除
     */
    @PostMapping("deleteById")
    @ResponseBody
    @PreAuthorize("hasRole('T_COMMON_CONFIG_DELETE')")
    public String deleteById(@RequestParam(required = false) Long id) {
        if (id == null) {
            return "-1";
        }
        return tCommonConfigService.deleteById(id);

    }


    /**
     * 根据主键修改
     */
    @PostMapping("update")
    @ResponseBody
    @PreAuthorize("hasRole('T_COMMON_CONFIG_UPDATE')")
    public String update(@RequestParam(required = false) Long id,
                         @RequestParam(required = false) String type,
                         @RequestParam(required = false) String typeRemark,
                         @RequestParam(required = false) String keys,
                         @RequestParam(required = false) String keysRemark,
                         @RequestParam(required = false) String value,
                         @RequestParam(required = false) String valuesRemark) {
        if (id == null) {
            return "-1";
        }
        if (type == null || typeRemark == null || keys == null ||
                keysRemark == null || value == null || valuesRemark == null) {
            return "-1";
        }
        return tCommonConfigService.updateConfigById(id, type, typeRemark, keys, keysRemark, value, valuesRemark);
    }

    /**
     * 有效无效
     */
    @PostMapping("updateState")
    @ResponseBody
    @PreAuthorize("hasRole('T_COMMON_CONFIG_UPDATE_STATE')")
    public String updateState(@RequestParam(required = false) Long id,
                              @RequestParam(required = false) Integer state) {
        if (id == null) {
            return "-1";
        }
        if (state == null) {
            return "-1";
        }
        return tCommonConfigService.updateState(id, state);
    }

}
